<template>
	<view class="j-v-box">
		<view class="j-v-image">
			<template v-if="hbImg">
				<image :src="hbImg" mode="aspectFill"></image>
			</template>
			<template v-else>
				<u-icon name="photo" color="#c5c8ce" size="80"></u-icon>
			</template>
		</view>
		<view class="j-v-account-box">
			<view class="j-v-account-bg">{{ hbAcc ? hbAcc : '暂未设置' }}</view>
		</view>
		<view class="j-v-account-btn">
			<view class="j-v-account-btn-b"><u-button @click="saveImage">保存图片</u-button></view>
			<view class="j-v-account-btn-b"><u-button @click="copyInvitationCode">复制账户</u-button></view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			hbAcc: '',
			hbImg: ''
		};
	},
	onLoad() {
		// 配置管理-根据type查询配置信息列表
		this.postFindConfigByType();
	},
	methods: {
		// 配置管理-根据type查询配置信息列表
		postFindConfigByType() {
			this.$u.api
				.postFindConfigByType({
					configType: 'AdminVirtualAccount'
				})
				.then(res => {
					console.log(res, '[充值页面-根据配置类型查询列表]');
					res.data.forEach(v => {
						if (v.configKey == 'AdminVirtualAccountUrl') {
							this.hbAcc = v.configValue ? v.configValue : '';
						}
						if (v.configKey == 'AdminVirtualAccountImg') {
							this.hbImg = v.configValue ? v.configValue : '';
						}
					});
				})
				.catch(err => {
					console.log(err);
				});
		},
		// 保存图片到相册
		saveImage() {
			var pages = getCurrentPages();
			var page = pages[pages.length - 1];
			var bitmap = null;
			var currentWebview = page.$getAppWebview();
			bitmap = new plus.nativeObj.Bitmap('amway_img');
			// 将webview内容绘制到Bitmap对象中
			currentWebview.draw(
				bitmap,
				function() {
					console.log('截屏绘制图片成功');
					bitmap.save(
						'_doc/a.jpg',
						{},
						function(i) {
							// console.log('保存图片成功：' + JSON.stringify(i));
							uni.saveImageToPhotosAlbum({
								filePath: i.target,
								success: function() {
									bitmap.clear(); //销毁Bitmap图片
									uni.showToast({
										title: '保存图片成功',
										mask: false,
										duration: 1500
									});
								}
							});
						},
						function(e) {
							console.log('保存图片失败：' + JSON.stringify(e));
						}
					);
				},
				function(e) {
					console.log('截屏绘制图片失败：' + JSON.stringify(e));
				}
			);
			//currentWebview.append(amway_bit);
		},
		// 复制邀请码
		copyInvitationCode() {
			uni.setClipboardData({
				data: this.hbAcc,
				success: () => {}
			});
		}
	}
};
</script>

<style scoped lang="scss">
.j-v-box {
	padding: 25rpx;

	.j-v-account-btn {
		display: flex;
		align-items: center;
		margin-top: 20rpx;
		justify-content: space-between;

		.j-v-account-btn-b {
			width: 300rpx;
		}
	}

	.j-v-account-box {
		padding: 25rpx 0rpx;

		.j-v-account-bg {
			border-radius: 14rpx;
			color: #605f5f;
			padding: 25rpx;
			text-align: center;
			word-wrap: break-word;
			background-color: #f2f2f2;
		}
	}

	.j-v-image {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		height: 931rpx;
		overflow: hidden;
		border-radius: 14rpx;
		background-color: #f2f2f2;

		image {
			width: 100%;
			height: 100%;
		}
	}
}
</style>
